package com.main.service.handler;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.main.service.PersonService;
import com.main.utility.Constants;

@Component
public class ServiceHandlerProcessor {

	private ServiceHandler prevHandler;
	
	@Autowired
	JdbcServiceHandler jdbcServiceHandler;
	
	@Autowired
	JpaServiceHandler jpaServiceHandler;

	public void addHandler(ServiceHandler handler) {
		if (prevHandler != null) {
			prevHandler.setNext(handler);
		}
		prevHandler = handler;
	}
	
	public ServiceHandler getHandler() {
		return prevHandler;
	}
	
	@PostConstruct
	private void init() {
		this.addHandler(jdbcServiceHandler);
		this.addHandler(jpaServiceHandler);
	}
	
	public PersonService getService() {
		return jdbcServiceHandler.handleRequest(Constants.PERSISTANCE_TYPE);
	}
}
